import axios from 'axios'
/**
 * 发送请求工具类
 */

const instance = axios.create({
  // baseURL: process.env.NODE_ENV,
  baseURL: '/',
  timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(
  config => {
    // 请求统一加入token
    /*
    if (store.getters.token) {
      config.headers['X-Token'] = getToken()
    }
    */
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  response => {
    const { code, data } = response.data

    if (code === 0) {
      return data
    } else {
      console.log('server error: ', code)
    }
  },
  error => {
    // Message({
    //   message: error.message,
    //   type: 'error',
    //   duration: 5 * 1000
    // })
    return Promise.reject(error)
  }
)

export default instance
